import { BaseContext } from "koa";
import { getRepository, Repository } from "typeorm";
import { MemoLists } from "../entity/memo_lists";

export default class MemoController {
    public static async getMemoLists(ctx: BaseContext) {
        try {

            const MemoRepository: Repository<MemoLists> = getRepository(
                MemoLists
            );
            const pageIndex = ctx.query.pageIndex && ctx.query.pageIndex > 0 ? ctx.query.pageIndex-1 : 0;
            const pageSize = ctx.query.pageSize || 10;
            const [ memoLists , total ] = await MemoRepository.findAndCount({
                relations: ["noteDetail","noteEnum","noteOwnType","todo_details","todo_details.statusEnum"],
                where: {
                    user: ctx.query.userId,
                },
                order: {
                    created_timestamp: "DESC"
                },
                skip: pageIndex*pageSize,
                take: pageSize,
                cache: true,
            });
            ctx.status = 200;
            ctx.body = ctx.util.response(ctx.ERROR.ERRORCODE_SUCCESS,ctx.ERROR.ERRORMSG_SUCCESS,{
                memoLists,
                total
            });

        }catch(error) {
            ctx.status = 500;
            ctx.app.emit('error', {
                code: ctx.ERROR.ERRORCODE_SYSTEMERROR,
                message: ctx.ERROR.ERRORMSG_SYSTEMERROR
            }, ctx);

        }
    }
}